// pages/arbitration/create/index.js
import requestUtil from '../../../baseRequest.js'
import requestUrls from '../../../requestUrl.js'
import commonUtils from '../../../utils/util.js'
import { $Message } from '../../../components/base/index'

let openid = wx.getStorageSync('openid')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:openid,
    uploaderInfo: {
      title: '证据照片',
      files: [],
      count: 30,
      uploadUrl: requestUrls.baseUrl + requestUrls.uploadUrl.commons,
      isUpload: false,
      sourceType: 'camera', //'all/camera'
      attentionText: '1.上传不少于1张图片,图片必须拍照上传',
      uploadComplete: 'uploadCompleteListener'
    },
    transportConfirm:{},
    transportConfirmFormId:"",
  },
  
  getTransportConfirm(){
    if (!this.data.openid) {
      this.setData({
        openid: wx.getStorageSync('openid')
      })
    }
    let param = {
      'openid': this.data.openid,
      'id': this.data.transportConfirmFormId
    }
    let url = requestUrls.transportConfirm.load
    requestUtil.postRequest(url, param, (res) => {
      let info = res
      this.setData({
        transportConfirm: info
      })
    }, () => { })

  },
  /**处理表单提交事件 */
  submitForm(e) {
    if (this.checkForForm(e.detail.value)) {
      this.data.formData = e.detail.value
      this.setData({
        showSpin: true,
        ['uploaderInfo.isUpload']: true
      })
      // 返回顶部
      wx.pageScrollTo({
        scrollTop: 0,
      })
    }
  },
  /**监听图片上传完成情况*/
  uploadCompleteListener(e) {
    console.log("证据照片回调-->",e)
    let detail = e.detail
    let status = detail.status
    if (status || status == 'error') {
      this.setData({
        showSpin: false,
        ['uploaderInfo.isUpload']: false
      })
      this.showErrorMessage('图片上传有误')
      return 
    }
    this.data.formData.evidencePhotos = detail
    console.log(this.data.formData)
    this.save()
  },
  save(){
 
    let param = this.data.formData
    param.openid = this.data.openid
    param.transportConfirmFormId = this.data.transportConfirmFormId
    console.log("提交参数",param)
    requestUtil.postRequest(requestUrls.arbitration.add, param, (res) => {
      this.setData({
        showSpin: false
      })
      let id = res.id
      wx.showToast({
        title: '创建成功',
        complete: () => {
          wx.redirectTo({
            url: `/pages/arbitration/detail/index?id=${id}`
          })
        }
      })
    }, () => { })
  },
  /**表单校验 */
  checkForForm(data) {
    console.log(data);
    if (data.evidencePhotos.length < 1) {
      this.showErrorMessage('证据照片不少于1张')
      return false
    }
    if (!data.addr || data.addr == '') {
      this.showErrorMessage('被控人联系地址不能为空')
      return false
    }
    if(data.addr.indexOf(" ")>-1){
      this.showErrorMessage('被控人联系地址不能含有空格')
      return false
    }
    if(commonUtils.isEmoji(data.addr)){
      this.showErrorMessage('被控人联系地址不能含有表情字符')
      return false
    }
    if(data.description && commonUtils.isEmoji(data.description)){
      this.showErrorMessage('纠纷描述不能含有表情字符')
      return false
    }
    return true
  },
   
  /**显示错误提示 */
  showErrorMessage(str) {
    $Message({
      content: str,
      type: 'error'
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //  console.log("加载",options)
    this.setData({ transportConfirmFormId: options.transportConfirmFormId })
    this.getTransportConfirm()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})